﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Stores.Interface;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.DataAccess;
using Microhard.Plat.CRM.Stores.DTO;
using Microhard.Plat.CRM.Stores.Msg;


namespace Micrhard.FMCG.Server.Impl
{
    public class IspCategoryImpl : IIspCategory
    {
        public bool IsNameDuplicate(IspCategoryModel model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var cnt = db.CheckListTemplateCategories.Count(c => c.CheckListTemplate.CheckListTemplateID == model.TemplateID
                && c.Name == model.Name);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(IspCategoryModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var op = model.ToEntity();
            try
            {
                op.CreateTime = DateTime.Now;
                db.CheckListTemplateCategories.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, IspCategoryMsg.AddFailed);
                Log.Error("IIspCategory.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(IspCategoryModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplateCategories.SingleOrDefault(l => l.CheckListCategoryID == model.CheckListCategoryID);
            if (entity != null)
            {
                entity.Name = model.Name;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, IspCategoryMsg.UpdateFailed);
                Log.Error("IIspCategory.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplateCategories.Where(l => ids.Contains(l.CheckListCategoryID));
            foreach (var e in entity)
            {
                //模板是否在使用
                if (e.CheckListTemplateItems.Count == 0)
                {
                    db.CheckListTemplateCategories.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(IspCategoryMsg.NameIsInUsing, e.Name));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, IspCategoryMsg.DeleteFailed);
                    Log.Error("IIspCategory.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(IspCategoryModel model)
        {
            throw new NotImplementedException();
        }

        public IspCategoryModel GetModelByID(Guid id)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var mcmp = db.CheckListTemplateCategories.SingleOrDefault(c => c.CheckListCategoryID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public IspCategoryModel GetModelByModel(IspCategoryModel model)
        {
            throw new NotImplementedException();
        }

        public List<IspCategoryModel> Query(IspCategoryModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            IEnumerable<CheckListTemplateCategory> entities = from b in db.CheckListTemplateCategories
                                                              where b.CheckListTemplate.CompanyID == this.scope.EmpInfo.CompanyID
                                                              && b.TemplateID == Model.TemplateID
                                                              select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
            {
                List<IspCategoryModel> list = pagedEntities.Select(m => m.ToModel()).ToList();
                foreach (IspCategoryModel l in list)
                {
                    var t = db.CheckListTemplates.SingleOrDefault(n => n.CheckListTemplateID == l.TemplateID);
                    l.TemplateName = t.TemplateName;
                }
                return list;
            }
            return new List<IspCategoryModel>();
        }

        public List<IspCategoryModel> Query(IspCategoryModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public List<IspCategoryModel> GetAllCategory()
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            IEnumerable<CheckListTemplateCategory> entities = from b in db.CheckListTemplateCategories
                                                              select b;
            if (entities.Count() > 0)
            {
                return entities.Select(m => m.ToModel()).ToList();
            }

            return new List<IspCategoryModel>();
        }
    }
}
